package com.yhxd.modular.news.service;

import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.yhxd.modular.news.dao.ClassifyDao;
import com.yhxd.modular.news.entity.Classify;
import com.yhxd.modular.news.entity.QClassify;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息分类获取
 * @author lzl
 * @date 2020/12/24 11:16
 */
@Service
public class ClassifyService {

    @Autowired
    private JPAQueryFactory factory;

    @Autowired
    private ClassifyDao classifyDao;

    /**
     * 获取消息分类
     * @param pid (消息大类可以不传)
     * @param claType (0信息类型，1信息小类)
     * @return
     */
    public List<Classify> findClassify(Long pid,Integer claType){
        QClassify qClassify = QClassify.classify;

        Predicate where = qClassify.id.isNotNull();
        if (ObjectUtils.isNotEmpty(pid)){      //分类id
            where = ExpressionUtils.and(where, qClassify.pid.eq(pid));
        }
        if (ObjectUtils.isNotEmpty(claType)){      //分类id
            where = ExpressionUtils.and(where, qClassify.claType.eq(claType));
        }
        QueryResults<Classify> classifyList = this.factory
                .selectFrom(qClassify)
                .where(where)
                .fetchResults();
        return classifyList.getResults();
    }
}
